package com.stillalive.blogsystem.common.advice;

import com.stillalive.blogsystem.common.exception.BlogException;
import com.stillalive.blogsystem.pojo.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.HandlerMethodValidationException;

import java.io.IOException;

@ControllerAdvice
@ResponseBody
@Slf4j
public class ExceptionAdvice {
    @ExceptionHandler
    public Result exceptionHandler(Exception e) {
        log.error("发生异常, e:" + e.getMessage());
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler
    public Result blogException(BlogException e) {
        log.error("发生异常, e: {}" + e.getMessage());
        return Result.fail(e.getErrMsg());
    }

    @ExceptionHandler
    public Result exceprionHandler(HandlerMethodValidationException e){
        log.error("发生异常，e: {}" + e.getMessage());
        return Result.fail("参数校验失败..");
    }
    @ExceptionHandler
    public Result exceptionHandler(MethodArgumentNotValidException e){
        String msg = e.getBindingResult().getFieldError().getDefaultMessage();
        log.error("发生异常，e:{}", e.getMessage());
        return Result.fail(msg);
    }
}
